﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.SqlClient;
using System.Web;
using System.Web.Services;
using System.Web.Mvc;
using kysec_MVC.Models;
using Webdiyer.WebControls.Mvc;
using kysec.CR.Common;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

namespace kysec_MVC.Controllers
{
    [HandleError]
    public class UserController : Controller
    {
        /// <summary>
        /// 实现分页显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult InfoList(int? id)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("UserInfo_{0}",id));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    PagedList<kysec.RAL.EFToMySql.tb_user> orders = db.tb_user.OrderBy(c=>c.c_uq_userId).ToPagedList(id ?? 1, 3);
                    if (Request.IsAjaxRequest())
                        return PartialView("UCUserInfoList", orders);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("UserInfo_{0}", id),
                            orders,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_user>)o);
        }

        /// <summary>
        /// 实现查询数据，根据标题，模糊……
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public ActionResult Search(string username)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("username_{0}", username));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    PagedList<kysec.RAL.EFToMySql.tb_user> orders = (from c in db.tb_user
                                                 where SqlMethods.Like(c.c_vc_userName, username + "%")
                                                 orderby c.c_dt_regDate descending
                                                 select c).ToPagedList(1, 10);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                        string.Format("username_{0}", username),
                        orders,
                        1,
                        System.Web.Caching.CacheItemPriority.Normal);
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_user>)o);
        }


        public ActionResult AddUserInfo()
        {
            return View();
        } 

        /// <summary>
        /// 添加用户基本信息
        /// </summary>
        [HttpPost]
        public string AddUserInfo(kysec.RAL.EFToMySql.tb_user model)
        {
            string result = "0";
            result = kysec.Bll.User.userInfo.AddUserInfo(model);
            return result;
        }

        /// <summary>
        /// 判断是否存在该用户，保证用户号的唯一性
        /// </summary>
        [HttpPost]
        public bool getUser(string username)
        {
            bool result = false;
            result = kysec.Bll.User.common.getUser(username);
            return result;
        }

        /// <summary>
        /// 完成基本信息标志
        /// </summary>
        public bool isFinishInfo(string userId)
        {
            bool result = false;
            result = kysec.Bll.User.userInfo.isFinishInfo(userId);
            return true;
        }

        public ActionResult FinishUserInfo()
        {
            return View("FinishUserInfo");
        }

        /// <summary>
        /// 添加用户详细信息
        /// </summary>
        [HttpPost]
        public string FinishUserInfo(kysec.RAL.EFToMySql.tb_userinfo model,string userId)
        {
            string result = "0";

            result = kysec.Bll.User.userInfo.FinishUserInfo(model, userId);

            return result;
        }

        /// <summary>
        /// 查询用户的详细信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        public ActionResult Details(string id)
        {
            List<kysec.RAL.EFToMySql.vw_tb_user> list = new List<kysec.RAL.EFToMySql.vw_tb_user>();
            list = kysec.Bll.User.userInfo.DetailsFinished(id);
            return PartialView("Details", list);
        }

        /// <summary>
        /// 查询用户的基本信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        public ActionResult BasicInfo(string id)
        {            
            List<kysec.RAL.EFToMySql.tb_user> list = new List<kysec.RAL.EFToMySql.tb_user>();
            list = kysec.Bll.User.userInfo.DetailsNotFinished(id);
            return PartialView("BasicInfo", list);            
        }

        /// <summary>
        /// 删除或者恢复用户信息
        /// </summary>
        public string DeleteUserInfo(string userId, bool isDelete)
        {
            string result = "0";

            result = kysec.Bll.User.userInfo.DeleteUserInfo(userId, isDelete);

            if (result.Equals("1"))
                Response.Redirect("/User/InfoList");
            return result;
        }


        public ActionResult EditPwdInfo()
        {
            return View();
        }
        /// <summary>
        /// 判断旧密码是否正确
        /// </summary>
        [HttpPost]
        public bool PwdIsExit(string userId, string password)
        {
            bool isExit = true;
            isExit = kysec.Bll.User.userInfo.PwdIsRight(userId, password);
            return isExit;
        }
        /// <summary>
        /// 修改用户密码信息
        /// </summary>
        [HttpPost]
        public string EditPwdInfo(string userId, string password)
        {
            string result = "0";

            result = kysec.Bll.User.userInfo.EditPwdInfo(userId, password);

            return result;
        }

        
        /// <summary>
        /// 修改基本信息
        /// </summary>
        public ActionResult EditBasicInfo(string id)
        {
            List<kysec.RAL.EFToMySql.tb_user> list = new List<kysec.RAL.EFToMySql.tb_user>();
            list = kysec.Bll.User.userInfo.DetailsNotFinished(id);
            return PartialView("EditBasicInfo",list);
        }
        [HttpPost]
        public string EditBasicInfo(string userId, kysec.RAL.EFToMySql.tb_user model)
        {
            string result = "0";

            result = kysec.Bll.User.userInfo.EditBasicInfo(userId, model);

            return result;
        }

        /// <summary>
        /// 修改详细信息
        /// </summary>
        public ActionResult EditDetailInfo(string id)
        {
            List<kysec.RAL.EFToMySql.vw_tb_user> list = new List<kysec.RAL.EFToMySql.vw_tb_user>();

            list = kysec.Bll.User.userInfo.DetailsFinished(id);

            return PartialView("EditDetailInfo",list);
        }

        [HttpPost]
        public string EditDetailInfo(string id, kysec.RAL.EFToMySql.tb_userinfo model)
        {
            string result = "0";

            result = kysec.Bll.User.userInfo.FinishUserInfo(model,id);

            return result;
            
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpLoad(string userIdImg)
        {
            HttpFileCollectionBase file = Request.Files;

            for (int i = 0; i < file.Count; i++)
            {
                //判断UserPhoto文件夹是否存在，不存在就创建
                string path = Server.MapPath("..\\Content\\back\\UserPhoto");
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                path = AppDomain.CurrentDomain.BaseDirectory + "Content/back/UserPhoto/";
                //获取上传的文件名
                string fileName = Path.GetFileName(file[i].FileName);
                string imgPath ="/Content/back/UserPhoto/"+fileName;
                file[i].SaveAs( Path.Combine(path, fileName));
                kysec.Bll.User.userInfo.photoUpLoad(userIdImg, imgPath);

            }           
            return Content("<script>alert('上传文件成功');window.history.back();</script>");
        }
    }
}
